<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\heepay\controller;

class CallBack extends \app\ApiRest
{
    protected $app;
    public function __construct(\think\App $app)
    {
        $this->app = $app;
    }
    public function companyCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        foreach ($inputs as $k => $value) {
            $input[$k] = iconv("gbk", "utf-8", urldecode($value));
        }
        $insert = ["uniacid" => $this->_uniacid, "text" => serialize($input), "type" => "company"];
        $callback_model = new \app\heepay\model\Callback();
        $callback_model->dataAdd($insert);
        $member_model = new \app\heepay\model\Member();
        $dis = ["apply_no" => $input["apply_no"]];
        $update = [];
        if(isset($input["notify_type"])) {
            $audit_status = in_array($input["notify_type"], [1, 2]) ? 1 : $input["notify_type"];
            $update = ["audit_status" => $audit_status];
        }
        if(isset($input["reject_reason"])) {
            $update["reject_reason"] = $input["reject_reason"];
        }
        $member_model->dataUpdate($dis, $update);
        $res = ["code" => 10000];
        echo json_encode($res);
        exit;
    }
    public function refundCallback()
    {
        $this->request = $this->app->request;
        $inputs = $this->request->param();
        if(empty($inputs)) {
            $inputs = $_GET;
        }
        foreach ($inputs as $k => $value) {
            $input[$k] = iconv("gbk", "utf-8", urldecode($value));
        }
        $insert = ["uniacid" => $this->_uniacid, "text" => @serialize($input), "type" => "refund", "heepay_order_code" => $input["agent_refund_bill_no"]];
        $callback_model = new \app\heepay\model\Callback();
        $callback_model->dataAdd($insert);
        $refund_model = new \app\massage\model\RefundOrder();
        $log_model = new \app\massage\model\OrderPrice();
        $record_model = new \app\heepay\model\RecordList();
        $record = $record_model->dataInfo(["heepay_order_code" => $_GET["agent_refund_bill_no"]]);
        $agent_refund_bill_no = !empty($_GET["hy_bill_no"]) ? $_GET["hy_bill_no"] : "";
        if(!empty($record)) {
            if(strtolower($_GET["refund_status"]) == "success") {
                $record_model->dataUpdate(["id" => $record["id"]], ["status" => 2]);
                $cash = $_GET["refund_amt"];
                if(!empty($record["order_id"])) {
                    $refund_model->where(["id" => $record["order_id"]])->update(["have_price" => \think\facade\Db::Raw("have_price+" . $cash), "out_refund_no" => $agent_refund_bill_no]);
                    $refund_order = $refund_model->dataInfo(["id" => $record["order_id"]]);
                    if(round($refund_order["refund_price"], 2) <= round($refund_order["have_price"], 2) && $refund_order["status"] == 4) {
                        $refund_model->passOrderData($record["order_id"], $refund_order["have_price"], 2, $refund_order["check_user"], 0, 2, 0, $refund_order["is_admin"]);
                    }
                }
            }
            if(strtolower($_GET["refund_status"]) == "fail") {
                $refund_model->dataUpdate(["id" => $record["order_id"]], ["status" => 5]);
                $record_model->where(["id" => $record["pay_record_id"]])->update(["true_price" => \think\facade\Db::raw("true_price+" . $cash)]);
                $log_model->where(["id" => $record["log_id"]])->update(["can_refund_price" => \think\facade\Db::raw("can_refund_price+" . $cash)]);
            }
        }
        echo "ok";
    }
    public function upOrderRefundCallback()
    {
        $insert = ["uniacid" => $this->_uniacid, "text" => serialize($_GET), "type" => "up_refund", "heepay_order_code" => $_GET["agent_refund_bill_no"]];
        $callback_model = new \app\heepay\model\Callback();
        $callback_model->dataAdd($insert);
        $pay_model = new \app\heepay\model\RecordList();
        $refund_model = new \app\massage\model\UpRefundOrder();
        $record = $pay_model->dataInfo(["heepay_order_code" => $_GET["agent_refund_bill_no"]]);
        $agent_refund_bill_no = !empty($_GET["hy_bill_no"]) ? $_GET["hy_bill_no"] : "";
        $cash = $_GET["refund_amt"];
        if(strtolower($_GET["refund_status"]) == "success") {
            $pay_model->dataUpdate(["adapay_id" => $record["id"]], ["status" => 2]);
            $refund_model->dataUpdate(["id" => $record["order_id"]], ["status" => 2, "have_price" => $cash, "transaction_id" => $agent_refund_bill_no]);
        }
        if(strtolower($_GET["refund_status"]) == "fail") {
            $refund_model->dataUpdate(["id" => $record["order_id"]], ["status" => 5]);
            $pay_model->where(["id" => $record["pay_record_id"]])->update(["true_price" => \think\facade\Db::raw("true_price+" . $cash)]);
        }
        return true;
    }
    public function payCallback()
    {
        $config_model = new \app\heepay\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $callback_model = new \app\heepay\model\Callback();
        $this->request = $this->app->request;
        $_GET["remark"] = iconv("gbk", "utf-8", urldecode($_GET["remark"]));
        $insert = ["uniacid" => $this->_uniacid, "text" => serialize($_GET), "type" => $_GET["remark"], "heepay_order_code" => $_GET["agent_bill_id"]];
        $callback_model->dataAdd($insert);
        $result = $_GET["result"];
        $agent_id = $_GET["agent_id"];
        $jnet_bill_no = $_GET["jnet_bill_no"];
        $agent_bill_id = $_GET["agent_bill_id"];
        $pay_type = $_GET["pay_type"];
        $pay_amt = $_GET["pay_amt"];
        $remark = $_GET["remark"];
        $returnSign = $_GET["sign"];
        $key = $config["pay_key"];
        $signStr = "";
        $signStr = $signStr . "result=" . $result;
        $signStr = $signStr . "&agent_id=" . $agent_id;
        $signStr = $signStr . "&jnet_bill_no=" . $jnet_bill_no;
        $signStr = $signStr . "&agent_bill_id=" . $agent_bill_id;
        $signStr = $signStr . "&pay_type=" . $pay_type;
        $signStr = $signStr . "&pay_amt=" . $pay_amt;
        $signStr = $signStr . "&remark=" . $remark;
        $signStr = $signStr . "&key=" . $key;
        $Verify_array = $signStr;
        $sign = $returnSign;
        $Verify_Sign = md5($Verify_array);
        $record_model = new \app\heepay\model\RecordList();
        $agent_bill_id = $record_model->where(["heepay_order_code" => $agent_bill_id])->value("order_code");
        $record_model->dataUpdate(["order_code" => $agent_bill_id], ["status" => 2]);
        if($sign == $Verify_Sign) {
            if($_GET["remark"] == "Balance") {
                $order_model = new \app\massage\model\BalanceOrder();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "Massage") {
                $order_model = new \app\massage\model\Order();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "MassageUp") {
                $order_model = new \app\massage\model\UpOrderList();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "Adapay") {
                $order_model = new \app\adapay\model\AdminPay();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "ResellerPay") {
                $order_model = new \app\payreseller\model\Order();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "AgentRecharge") {
                $order_model = new \app\mobilenode\model\RechargeList();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "Memberdiscount") {
                $order_model = new \app\memberdiscount\model\OrderList();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            } elseif($_GET["remark"] == "Balancediscount") {
                $order_model = new \app\balancediscount\model\OrderList();
                $order_model->orderResult($agent_bill_id, $jnet_bill_no);
            }
            echo "ok";
        } else {
            echo "error";
        }
    }
    public function walletCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        foreach ($inputs as $k => $value) {
            $input[$k] = iconv("gbk", "utf-8", urldecode($value));
        }
        $insert = ["uniacid" => $this->_uniacid, "text" => serialize($input), "type" => "wallet", "heepay_order_code" => $input["out_trade_no"]];
        $callback_model = new \app\heepay\model\Callback();
        $callback_model->dataAdd($insert);
        $wallet_model = new \app\massage\model\Wallet();
        $dis = ["payment_no" => $input["out_trade_no"]];
        $record = $wallet_model->dataInfo($dis);
        if($input["bill_status"] == "1" && !empty($record)) {
            $wallet_model->dataUpdate(["id" => $record["id"]], ["status" => 2]);
        }
        if($input["bill_status"] == "-1" && !empty($record)) {
            $wallet_model->dataUpdate(["id" => $record["id"]], ["status" => 5]);
        }
        echo "ok";
        exit;
    }
    public function rechargeCallBack()
    {
        $this->request = $this->app->request;
        $inputs = $this->request->param();
        if(empty($inputs)) {
            $inputs = $_GET;
        }
        foreach ($inputs as $k => $value) {
            $input[$k] = iconv("gbk", "utf-8", urldecode($value));
        }
        $insert = ["uniacid" => $this->_uniacid, "text" => serialize($input), "type" => "recharge", "heepay_order_code" => $input["batch_no"]];
        $callback_model = new \app\heepay\model\Callback();
        $callback_model->dataAdd($insert);
        $record_model = new \app\heepay\model\RechargeRecord();
        $record = $record_model->dataInfo(["agent_bill_id" => $input["batch_no"]]);
        if(!empty($record) && $input["status"] == 1) {
            $record_model->dataUpdate(["id" => $record["id"]], ["status" => 2]);
        }
        if(!empty($record) && $input["status"] == -1) {
            $record_model->dataUpdate(["id" => $record["id"]], ["status" => 3]);
        }
        echo "ok";
        exit;
    }
}

?>